﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WoChu.RfClient.Entity;
using WoChu.RfClient.Common;
using WoChu.CF.BaseFrame.Util;

namespace WoChu.RfClient.Duty
{
    public partial class FrmOrderCheck : Form
    {
        public FrmOrderCheck()
        {
            InitializeComponent();
        }


        private static List<OrderItemInfo> lstOrderItem = null;
 

        private void FrmOrderCheck_Load(object sender, EventArgs e)
        {

        }

        private void txtCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;

            try
            {
                string _Code = txtCode.Text.Trim();
                if (string.IsNullOrEmpty(_Code))
                {
                    MessageBox.Show("订单号不能为空！");
                    return;
                }

                Cursor.Current = Cursors.WaitCursor;

                lstOrderItem = Duty.OrderCheckUIRule.NewInstance.Query(_Code);

                if (lstOrderItem != null && lstOrderItem.Count > 0)
                {
                    this.OrderCheckSource.DataSource = lstOrderItem;
                    this.dbGridOrderCheck.DataSource = this.OrderCheckSource;

                    OrderInfo _order = new OrderInfo();
                    _order.ORDERID = lstOrderItem[0].SHEETID;
                    _order.QTYCOUNT = lstOrderItem.Count;
                    _order.QTYSUM = (from orderItme in lstOrderItem select orderItme.REALQTY).Sum();

                    Duty.OrderCheckUIRule.NewInstance.InsertPDACheckOrder(LoginBean.Instance.USERCODE, _order);

                    txtOrderNo.Text = _order.ORDERID;
                    txtSum.Text = _order.QTYSUM.ToString();
                    txtCount.Text = _order.QTYCOUNT.ToString();
                }
                else
                {
                    this.OrderCheckSource.DataSource = null;
                    this.dbGridOrderCheck.DataSource = this.OrderCheckSource;

                    txtOrderNo.Text = string.Empty;
                    txtSum.Text = string.Empty;
                    txtCount.Text = string.Empty;
                }

                this.dbGridOrderCheck.Refresh();
                txtCode.Text = string.Empty;
                txtCode.Focus();
            }
            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                if (lstOrderItem != null && lstOrderItem.Count > 0)
                {
                    this.OrderCheckSource.DataSource = lstOrderItem;
                    this.dbGridOrderCheck.DataSource = this.OrderCheckSource;

                    OrderInfo _order = new OrderInfo();
                    _order.ORDERID = lstOrderItem[0].SHEETID;
                    _order.QTYCOUNT = lstOrderItem.Count;
                    _order.QTYSUM = (from orderItme in lstOrderItem select orderItme.REALQTY).Sum();

                    Duty.OrderCheckUIRule.NewInstance.UpdatePDACheckOrder(LoginBean.Instance.USERCODE, _order);

                    txtOrderNo.Text = string.Empty;
                    txtSum.Text = string.Empty;
                    txtCount.Text = string.Empty;
                    txtCode.Text = string.Empty;
                    txtCode.Focus();
                }             
            }
            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void FrmOrderCheck_Closed(object sender, EventArgs e)
        {
            //System.Reflection.Assembly ass = System.Reflection.Assembly.Load("WoChu.RfClient.Login");
            //Type type = ass.GetType("WoChu.RfClient.Login.FrmMain");
            //Object o = Activator.CreateInstance(type);
            //System.Reflection.MethodInfo mi = type.GetMethod("Show");

            //mi.Invoke(o, null);

            this.Close();
            this.Dispose();
        }

        private void FrmOrderCheck_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                if (MessageBoxEx.Question("确定退出吗？") == DialogResult.Yes)
                {
                    //System.Reflection.Assembly ass = System.Reflection.Assembly.Load("WoChu.RfClient.Login");
                    //Type type = ass.GetType("WoChu.RfClient.Login.FrmMain");
                    //Object o = Activator.CreateInstance(type);
                    //System.Reflection.MethodInfo mi = type.GetMethod("Show");

                    //mi.Invoke(o, null);

                    this.Close();
                    this.Dispose();
                }
            }
        }

    }
}